
//
//  NewsInfoTopHeaderView.m
//  GuShiNews
//
//  Created by huachun on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "NewsInfoTopHeaderView.h"
#import "NewsModel.h"
#import "XLPhotoBrowser.h"

@interface NewsInfoTopHeaderView ()

@property (nonatomic, strong) UILabel *Tittle;
@property (nonatomic, strong) UILabel *Label_bottom;
@property (nonatomic, strong) CALayer *line;
@property (nonatomic, strong) UIImageView *News_image;
@property (nonatomic, strong) UIButton    *News_button;

@end

@implementation NewsInfoTopHeaderView

/* 在构造方法中，创建UI*/
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        _Tittle = [[UILabel alloc] initWithFrame:CGRectMake(25, 15, RECRWITCH-50, 80)];
        _Tittle.font      = [UIFont boldSystemFontOfSize:23];
        _Tittle.textColor = NewsTitleColor;
        _Tittle.numberOfLines = 0 ;
        _Tittle.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_Tittle];
        
        
        _Label_bottom           = [[UILabel alloc] init];
        _Label_bottom.font      = [UIFont systemFontOfSize:13];
        _Label_bottom.textColor = [JonesClass stringTOColor:@"#888888"];
        _Label_bottom.text = @"本站 · 20分钟前";
        _Label_bottom.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_Label_bottom];
        
        _line = [[CALayer alloc] init];
        _line.backgroundColor=LineColor.CGColor;
        _line.cornerRadius=2.0;
        _line.masksToBounds=YES;
        [self.layer addSublayer:_line];
        
        _News_image                     = [[UIImageView alloc] init];
        _News_image.layer.masksToBounds = YES;
        _News_image.layer.cornerRadius  = 5;
        _News_image.contentMode         = UIViewContentModeScaleAspectFill;
        _News_image.backgroundColor = backColor;
        [self addSubview:_News_image];
        
        _News_button = [[UIButton alloc] init];
        [_News_button addTarget:self action:@selector(showimage:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_News_button];
    }
    return self;
}

- (void)showimage:(UIButton *)sender{
    
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:@[_Model.news_img] currentImageIndex:0];
    browser.seletBlock = ^(BOOL tui_ok) {
        
    };
}

- (void)setModel:(NewsModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
    
    NSDictionary *dic = @{NSKernAttributeName:@0.f};
    NSMutableAttributedString * attributedString =     [[NSMutableAttributedString alloc] initWithString:_Model.news_tittle attributes:dic];
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]     init];
    [paragraphStyle setLineSpacing:4];
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [_Model.news_tittle length])];
    [_Tittle setAttributedText:attributedString];
    [_Tittle sizeToFit];
    
    _Tittle.frame= CGRectMake(25, 15, RECRWITCH-50, _Model.title_height);
    _Label_bottom.frame = CGRectMake(20, _Model.title_height+25, _Tittle.frame.size.width, 30);
    _line.frame=CGRectMake(RECRWITCH/2-35, _Model.title_height+70, 70, 2);
    _News_image.frame=CGRectMake(25, _Model.title_height+95, RECRWITCH-50, (RECRWITCH-50)/3*2);
    _News_button.frame=_News_image.frame;
    
    if (_Model.news_show_type==0) {
        _News_image.hidden=YES;
        _News_button.hidden = YES;
    }else {
        _News_image.hidden=NO;
        _News_button.hidden = NO;
        [_News_image sd_setImageWithURL:[NSURL URLWithString:_Model.news_img]];
    }
    
    
    NSString *bottom_string;
//    if ([_Model.news_count intValue]>0) {
//        bottom_string = [NSString stringWithFormat:@"%@  作者：%@    %@评论  %@",_Model.news_come,_Model.news_write, _Model.news_count,[JonesClass timeZhuan:_Model.news_date]];
//    }else{
//        bottom_string = [NSString stringWithFormat:@"%@  作者：%@  %@",_Model.news_come,_Model.news_write,[JonesClass timeZhuan:_Model.news_date]];
//    }
    
    if (_Model.news_write.length>0) {
        bottom_string = [NSString stringWithFormat:@"%@  作者：%@  %@",_Model.news_come,_Model.news_write,[JonesClass timeZhuan:_Model.news_date]];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:bottom_string];
        [str addAttribute:NSForegroundColorAttributeName value:[JonesClass stringTOColor:@"#555555"] range:NSMakeRange(0,_Model.news_come.length+_Model.news_write.length+6)];
        _Label_bottom.attributedText = str;
    }else{
        bottom_string = [NSString stringWithFormat:@"%@  %@",_Model.news_come,[JonesClass timeZhuan:_Model.news_date]];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:bottom_string];
        [str addAttribute:NSForegroundColorAttributeName value:[JonesClass stringTOColor:@"#555555"] range:NSMakeRange(0,_Model.news_come.length+1)];
        _Label_bottom.attributedText = str;
    }

}


@end
